//
//  DateArea.swift
//  FeastBooking
//
//  Created by Imon on 2017/7/14.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

protocol DateAreaDelegate {
    func addDate(textfield: UITextField)
    func pickADate(textfield: UITextField)
    func deleteDate()
    func maxDate()
}

class DateArea: UIView {

    var amount: Int = 1
    
    let max: Int = 5
    
    var delegate: DateAreaDelegate?
    
    var textfields: [UITextField] = {
        var tfs: [UITextField] = []
        let tf = UITextField()
        tf.placeholder = LanguageHelper.getString(key: "CAN_DATE")
        tf.tintColor = UIColor.black        
        tf.font = AppFont(size: 14)
        tf.isEnabled = false
        tfs.append(tf)
        return tfs
    }()
    
    var saperators: [UIView] = []
    
    let titleLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = UIColor.black
        label.text = LanguageHelper.getString(key: "CAN_DATE")
        return label
    }()
    
    let addButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "add_date"), for: .normal)
        return button
    }()

    let deleteButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "delete_date"), for: .normal)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(titleLabel)
        self.addSubview(addButton)
        self.addSubview(deleteButton)
        addButton.addTarget(self, action: #selector(addDate(_:)), for: .touchUpInside)
        deleteButton.addTarget(self, action: #selector(deleteDate(_:)), for: .touchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let point = touch.location(in: self)
            for tf in textfields {
                if tf.frame.contains(point) {
                    self.delegate?.pickADate(textfield: tf)
                }
            }
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let textMaxSize = CGSize(width: bounds.width, height: 20)
        let titleWidth = TextSize.textSize(text: titleLabel.text!, font: AppFont(size: 14), maxSize: textMaxSize).width
        titleLabel.frame = CGRect(x: 10, y: 10, width: titleWidth, height: 30)
        addButton.frame = CGRect(x: titleWidth + 25, y: 14 + menuHeight * CGFloat(amount - 1), width: 20, height: 20)
        for i in 0...textfields.count - 1 {
            textfields[i].frame = CGRect(x: addButton.frame.origin.x + addButton.frame.width + 20, y: 10 + menuHeight * CGFloat(i) , width: 190, height: 30)
            let saperator = UIView()
            saperator.backgroundColor = lightSaperatorColor
            saperator.frame = CGRect(x: 0, y: 49.5 + menuHeight * CGFloat(i), width: self.bounds.width, height: 0.5)
            self.addSubview(textfields[i])
            self.addSubview(saperator)
            saperators.append(saperator)
        }
        deleteButton.frame = CGRect(x: bounds.width - 20 - 5 , y: 15 + menuHeight * CGFloat(amount - 1), width: 20, height: 20)
        deleteButton.isHidden = amount > 1 ? false : true
        
    }
    
    func addDate(_ sender: UIButton) {
        if amount == max {
            delegate?.maxDate()
            return
        }
        let tf = UITextField()
        tf.placeholder = LanguageHelper.getString(key: "CAN_DATE")
        tf.tintColor = UIColor.black
        tf.font = AppFont(size: 14)
        tf.isEnabled = false
        textfields.append(tf)
        amount += 1
        delegate?.addDate(textfield: tf)
    }
    
    func deleteDate(_ sender: UIButton) {
        let tf = textfields.popLast()
        tf?.removeFromSuperview()
        let s = saperators.popLast()
        s?.removeFromSuperview()
        amount -= 1
        delegate?.deleteDate()
    }
}
